package com.xsmart.oa.services.mall.mymenu;

import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.mall.mymenu.PwdManagerDao;
import com.xsmart.oa.dao.mall.mymenu.RegisterDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class PwdManagerService {
    @Autowired
    RegisterDao registerDao;
    @Autowired
    PwdManagerDao pwdManagerDao;

    /**
     * 忘记密码
     * @param requestBean
     * @return
     */
    public ResponseBean forgetPwd(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String userName = requestBean.getRequestParamString("userName");

        Map<String,Object> user = registerDao.findByUsername(userName);
        if (user != null){
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("userInfo", user);
        }else {
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 修改密码
     * @param requestBean
     * @return
     */
    public ResponseBean updatePwd(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();

        String userId = requestBean.getRequestParamString("userId");
        String password = requestBean.getRequestParamString("newPwd");

        int num = pwdManagerDao.updatePwd(userId,password);
        if (num > 0){
            responseBean.setResult(ResultConstant.SUCCESS);
        }else {
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 重置密码
     * @param requestBean
     * @return
     */
    public ResponseBean setPwd(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();

        String userName = requestBean.getRequestParamString("userName");
        String password = requestBean.getRequestParamString("newPwd");

        int num = pwdManagerDao.setPwd(userName,password);
        if (num > 0){
            responseBean.setResult(ResultConstant.SUCCESS);
        }else {
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }
}
